<?php
namespace app\agent\logic;

use app\common\model\WkSellerModel;

class WkSellerLogic extends WkSellerModel
{
	
	public function getPage($where = [], $request = [])
	{
	    $where[] = ['seller.status', '=', '1'];

		//排序
		$order = [
		    $this->getPk() => 'DESC',
		];

		$field = [
		    'seller.*',
		    'info.mch_phone', 'info.address', 'info.checkTime',
            'tginfo.truename', 'tginfo.tgnumber',
            'user.telephone',
        ];

		//join
        $join = [
            ['wkSellerinfo info', 'info.uid = seller.uid'],
            ['user_tginfo tginfo', 'tginfo.uid = seller.bind_uid'],
            ['user', 'user.uid = tginfo.uid'],
        ];

		//查询数据
		$data = $this->alias('seller')->field($field)->join($join)->where($where)->order($order)->paginate(10, false, ['query' => $request]);

		//字段属性映射
		foreach ($data as $key => &$val)
		{
			$this->attributeMap($val);
		}
		
		return $data;
	}

	public function getExcel($where = [], $request = [])
	{
	    $where[] = ['seller.status', '=', '1'];

		//排序
		$order = [
		    $this->getPk() => 'DESC',
		];

		$field = [
		    'seller.*',
		    'info.*',
            'tginfo.truename','tginfo.tgnumber',
            'user.telephone',
        ];

		//join
        $join = [
            ['wkSellerinfo info', 'info.uid = seller.uid', 'LEFT'],
            ['user_tginfo tginfo', 'tginfo.uid = seller.bind_uid', 'LEFT'],
            ['user', 'user.uid = tginfo.uid', 'LEFT'],
        ];

		//查询数据
		$data = $this->alias('seller')->field($field)->join($join)->where($where)->order($order)->select();

		//字段属性映射
		foreach ($data as $key => &$val)
		{
			$this->attributeMap($val);
		}

		return $data;
	}

    /**
     * 获取所有符合条件商家的uid
     * @param $where
     * @return array
     */
    public function getSellerUids($where)
    {
        return $this->where($where)->column('uid');
	}

	public function updateRecord($data = [])
	{
		//入库
		$data = $this->allowField(true)->save($data, [$this->getPk() => $data['id']]);
		
		if( $data === false )
		{
			return false;
		}
		
		return true;
	}

	public function getDetail($where = [])
	{
        //join
        $join = [
            ['wkSellerinfo info', 'info.uid = seller.uid'],
        ];

		//查询数据
		$data = $this->alias('seller')->join($join)->where($where)->find();

		//字段属性映射
		$data = $this->attributeMap($data);

		return $data;
	}

    /**
     * 获取代理商辖区内的所有商家
     * @param $type int 代理商级别
     * @param $addr_code int 辖区
     * @return float|string
     */
    public function getSellersByType($type, $addr_code)
    {
        $category = $this->_map['type'][$type];

        $where = [
            ['info.' . $category, '=', $addr_code],
            ['seller.status', '=', 1],
            ['seller.uid', '<>', `sellerex.uid`]
        ];

        $join = [
            ['wkSellerinfo info', 'info.uid = seller.uid'],
            ['wkSellerex sellerex', "sellerex.{$category} = info.{$category}"],
        ];

        return $this->alias('seller')->join($join)->where($where)->count('seller.uid');
	}


    /**
     * 获取辖区商家营业额
     * @param $data
     * @param $tableObj
     */
    public function getSellersProfit($where, $tableObj, $field = [])
    {
        if( empty($field) )
        {
            $field = 'sum(pday) pday, sum(pmonth) pmonth, sum(pyear) pyear, sum(pzong) pzong';
        }

        $join = [
            ['wkSellerex sellerex', 'sellerex.uid = statistics.uid'],
        ];

        return $tableObj->alias('statistics')->field($field)->join($join)->where($where)->find()->toArray();

    }

    /**
     * @action 获取辖区商家积分营业额
     */
    public function getSellersWljfProfit($where, $tableObj)
    {
        $field = 'sum(zong) zong';

        return $this->getSellersProfit($where, $tableObj, $field);
    }
}